using System.Text.Json.Serialization;

namespace KeyAuthService.Models
{
    /// <summary>
    /// WebSocket请求消息
    /// </summary>
    public class WebSocketRequest
    {
        /// <summary>
        /// 操作类型
        /// </summary>
        [JsonPropertyName("action")]
        public string Action { get; set; } = string.Empty;

        /// <summary>
        /// 参数
        /// </summary>
        [JsonPropertyName("params")]
        public object? Params { get; set; }

        /// <summary>
        /// 请求ID
        /// </summary>
        [JsonPropertyName("request_id")]
        public string RequestId { get; set; } = string.Empty;
        
        /// <summary>
        /// 客户端ID (WebSocket连接ID)
        /// </summary>
        [JsonIgnore] // 不需要从JSON反序列化，由服务端设置
        public string ClientId { get; set; } = string.Empty;
    }

    /// <summary>
    /// WebSocket响应消息
    /// </summary>
    public class WebSocketResponse
    {
        /// <summary>
        /// 请求ID
        /// </summary>
        [JsonPropertyName("request_id")]
        public string RequestId { get; set; } = string.Empty;

        /// <summary>
        /// 状态
        /// </summary>
        [JsonPropertyName("status")]
        public string Status { get; set; } = "success";

        /// <summary>
        /// 数据
        /// </summary>
        [JsonPropertyName("data")]
        public object? Data { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        [JsonPropertyName("error")]
        public ErrorInfo? Error { get; set; }

        /// <summary>
        /// 创建成功响应
        /// </summary>
        public static WebSocketResponse Success(string requestId, object? data = null)
        {
            return new WebSocketResponse
            {
                RequestId = requestId,
                Status = "success",
                Data = data,
                Error = null
            };
        }

        /// <summary>
        /// 创建错误响应
        /// </summary>
        public static WebSocketResponse CreateError(string requestId, string code, string message)
        {
            return new WebSocketResponse
            {
                RequestId = requestId,
                Status = "error",
                Data = null,
                Error = new ErrorInfo { Code = code, Message = message }
            };
        }
    }

    /// <summary>
    /// 错误信息
    /// </summary>
    public class ErrorInfo
    {
        /// <summary>
        /// 错误代码
        /// </summary>
        [JsonPropertyName("code")]
        public string Code { get; set; } = string.Empty;

        /// <summary>
        /// 错误消息
        /// </summary>
        [JsonPropertyName("message")]
        public string Message { get; set; } = string.Empty;
    }
} 